package com.ruoyi.member.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.History;
import com.ruoyi.member.service.IHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 观看历史Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/history")
public class HistoryController extends BaseController
{
    @Autowired
    private IHistoryService historyService;

    /**
     * 查询观看历史列表
     */
    @PreAuthorize("@ss.hasPermi('member:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(History history)
    {
        startPage();
        List<History> list = historyService.selectHistoryList(history);
        return getDataTable(list);
    }

    /**
     * 新增观看记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody History history)
    {
        return toAjax(historyService.insertHistory(history));
    }

    /**
     * 更新观看进度
     */
    @PutMapping("/progress")
    public AjaxResult updateProgress(@RequestBody History history)
    {
        return toAjax(historyService.updateProgress(history));
    }

    /**
     * 删除观看历史
     */
    @PreAuthorize("@ss.hasPermi('member:history:remove')")
    @Log(title = "观看历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(historyService.deleteHistoryByIds(historyIds));
    }

    /**
     * 清空用户观看历史
     */
    @PreAuthorize("@ss.hasPermi('member:history:remove')")
    @Log(title = "清空观看历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/clear/{memberId}")
    public AjaxResult clearHistory(@PathVariable("memberId") Long memberId)
    {
        return toAjax(historyService.clearHistoryByMemberId(memberId));
    }

    /**
     * 获取用户观看历史
     */
    @GetMapping("/member/{memberId}")
    public AjaxResult getMemberHistory(@PathVariable("memberId") Long memberId)
    {
        History history = new History();
        history.setMemberId(memberId);
        List<History> list = historyService.selectHistoryList(history);
        return AjaxResult.success(list);
    }

    /**
     * 获取继续观看列表
     */
    @GetMapping("/continue/{memberId}")
    public AjaxResult getContinueWatching(@PathVariable("memberId") Long memberId)
    {
        List<History> list = historyService.getContinueWatchingList(memberId);
        return AjaxResult.success(list);
    }

    /**
     * 导出观看历史
     */
    @PreAuthorize("@ss.hasPermi('member:history:export')")
    @Log(title = "观看历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, History history)
    {
        List<History> list = historyService.selectHistoryList(history);
        ExcelUtil<History> util = new ExcelUtil<History>(History.class);
        util.exportExcel(response, list, "观看历史数据");
    }
}